package com.glory.hikvisionmobile.utils;

import android.content.Context;
import android.util.Log;

import androidx.fragment.app.FragmentManager;

import com.glory.hikvisionmobile.BuildConfig;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.net.BaseSelectorListBean;
import com.glory.hikvisionmobile.bean.net.XPdaPart;
import com.glory.hikvisionmobile.bean.net.xADrefListBean;
import com.glory.hikvisionmobile.bean.net.BaseRequestBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.bean.net.HeaderBean;
import com.glory.hikvisionmobile.bean.net.RequestBean;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_NetUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yi.zhang
 * @date 2019/9/2
 * @desc 网络请求处理类
 */
public class NetUtils {

    public interface SelectorCallback{
        void onSuccess(List<String> list);
        void onFail(Throwable e,String messageName);
    }

    public static <T> void requestDara(String method,Object tag,JsonObject jsonString,FreeUI_EntityCallBack<T> callBack){
        RequestBean format = new RequestBean();
        format.setBody(jsonString);
        format.setHeader(new HeaderBean(method));
        BaseRequestBean message = new BaseRequestBean();
        message.setRequest(format);
        Map<String,String> map = new LinkedHashMap<>();
        map.put("senderId","PDA2MESSender");
        map.put("messageName",method);
        map.put("transactionId","c5e5e318-389d-4c39-b914-e4c10ba519d4");
        map.put("requestMessage",new Gson().toJson(message));
        map.put("timeOut","5000");
        Log.e("requestDara", "requestDara: " + new Gson().toJson(message));
        FreeApi_NetUtils.requestGetNet((BuildConfig.DEBUG ? Constant.REQUEST_URL_DEBUG : Constant.REQUEST_URL) + "/esb/RestService/sendrequest?",tag,map,callBack);

    }

    public static void getSelectorList(String referenceName, String referencetype, Context tag, FragmentManager fragmentManager,SelectorCallback callback){
        JsonObject map = new JsonObject();
        map.addProperty("referenceName",referenceName);
        map.addProperty("referencetype",referencetype);
        requestDara("HIK.REFLIST",tag,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<BaseSelectorListBean>>(
                new TypeToken<BaseResponseBean<BaseSelectorListBean>>() {
                }.getType(), fragmentManager, tag){

            @Override
            public void onSuccess(Response<BaseResponseBean<BaseSelectorListBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(tag,true,fragmentManager)) {
                    List<xADrefListBean> xADrefListBeans = response.body().getBody().getxADrefList();
                    List<String> selectorList = new ArrayList<>();
                    selectorList.clear();
                    for (xADrefListBean bean : xADrefListBeans){
                        selectorList.add(bean.getText() + "/" + bean.getDescription());
                    }
                    callback.onSuccess(selectorList);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BaseSelectorListBean>> response) {
                super.onError(response);
                callback.onFail(response.getException(),"HIK.REFLIST");
            }
        });

    }

    public static void getLotTypeList(String referenceName, String referencetype, Context tag, FragmentManager fragmentManager,SelectorCallback callback){
        JsonObject map = new JsonObject();
        map.addProperty("referenceName",referenceName);
        map.addProperty("referencetype",referencetype);
        requestDara("HIK.REFLIST",tag,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<BaseSelectorListBean>>(
                new TypeToken<BaseResponseBean<BaseSelectorListBean>>() {
                }.getType(), fragmentManager, tag){

            @Override
            public void onSuccess(Response<BaseResponseBean<BaseSelectorListBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(tag,true,fragmentManager)) {
                    List<xADrefListBean> xADrefListBeans = response.body().getBody().getxADrefList();
                    List<String> selectorList = new ArrayList<>();
                    selectorList.clear();
                    for (xADrefListBean bean : xADrefListBeans){
                        selectorList.add(bean.getText() + "/" + bean.getDescription() + "/" + bean.getKEY());
                    }
                    callback.onSuccess(selectorList);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BaseSelectorListBean>> response) {
                super.onError(response);
                callback.onFail(response.getException(),"HIK.REFLIST");
            }
        });

    }

    public static void getPartSelectorList(Context tag, FragmentManager fragmentManager,SelectorCallback callback){
        JsonObject map = new JsonObject();
        map.addProperty("status","Active");
        requestDara("HIK.PdaPartList",tag,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<BaseSelectorListBean>>(
                new TypeToken<BaseResponseBean<BaseSelectorListBean>>() {
                }.getType(), fragmentManager, tag){

            @Override
            public void onSuccess(Response<BaseResponseBean<BaseSelectorListBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(tag,true,fragmentManager)) {
                    List<XPdaPart> xPdaParts = response.body().getBody().getXPdaParts();
                    List<String> selectorList = new ArrayList<>();
                    selectorList.clear();
                    for (XPdaPart bean : xPdaParts){
                        selectorList.add(bean.getName() + "/" + bean.getDescription());
                    }
                    callback.onSuccess(selectorList);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BaseSelectorListBean>> response) {
                super.onError(response);
                callback.onFail(response.getException(),"HIK.PdaPartList");
            }
        });

    }

    public static <T> void requestMapDara(String method,Object tag,Map bodyMap,FreeUI_EntityCallBack<T> callBack){
        RequestBean format = new RequestBean();
        format.setBody(bodyMap);
        format.setHeader(new HeaderBean(method));
        BaseRequestBean message = new BaseRequestBean();
        message.setRequest(format);
        Map<String,String> map = new LinkedHashMap<>();
        map.put("senderId","PDA2MESSender");
        map.put("messageName",method);
        map.put("transactionId","c5e5e318-389d-4c39-b914-e4c10ba519d4");
        String messageJson = new Gson().toJson(message);
        map.put("requestMessage", messageJson);
        map.put("timeOut","5000");
        Log.e("requestDara", "requestDara: " + messageJson);
        FreeApi_NetUtils.requestGetNet((BuildConfig.DEBUG ? Constant.REQUEST_URL_DEBUG : Constant.REQUEST_URL) + "/esb/RestService/sendrequest?",tag,map,callBack);

    }
}
